<!-- 表格组件 -->
<script lang="tsx" setup>
import { toRaw } from "vue";

import type { TableColumnOption } from "@/components/table-widget/table-widget.vue";
import TableWidget from "@/components/table-widget/table-widget.vue";
import {
	CREDIT_INFO_REPORT_REPORT_STATUS_OPTIONS,
	CREDIT_INFO_REPORT_TYPE_OPTIONS,
	getEnumOptionsField,
} from "@/config/enum-options";
import { whManagerBsTbCreditInfoReportDeleteByIdIdUsingPost } from "@/genapi/wh";

import { injectState } from "../state";

import { whManagerBsTbEnterpriseGetListUsingPost } from "@/genapi/wh";
import { computedLoading } from "@/hooks/computed-loading";

defineOptions({ name: "Sheet" });

const {
	selectedList,
	tableRef,
	detailDialogRef,
	page: { records, isLoading, execute },
} = injectState()!;

watch(records, () => tableRef.value?.table?.clearSelection());

const onDelete = async (row: (typeof records.value)[0]) => {
	await ElMessageBox.confirm(
		`您即将删除【${row.name ?? "未知"}】，删除后不可恢复，是否确认删除？`,
		{
			type: "error",
			title: "提示",
		},
	);
	await whManagerBsTbCreditInfoReportDeleteByIdIdUsingPost({
		path: { id: row.id! },
	});
	ElMessage.success("删除成功");
	await execute();
};

const [companyList] = computedLoading(async () => {
	const { data } = await whManagerBsTbEnterpriseGetListUsingPost({ data: {} });
	return data?.map((item) => ({ label: item.name, id: item.id })) ?? [];
});

const getCompanyName = (enterpriseId) => {
	const res = companyList.value?.filter((item) => {
		return item.id === enterpriseId;
	});
	return res[0] ? res[0].label : "--";
};

const columns: TableColumnOption<(typeof records.value)[0]>[] = [
	{
		label: "序号",
		type: "index",
		fixed: "left",
		width: 80,
	},
	{
		label: "信用信息类型",
		formatter: (row) =>
			getEnumOptionsField(CREDIT_INFO_REPORT_TYPE_OPTIONS, "label", row.type) ??
			"-",
	},
	{
		label: "信用信息详情",
		formatter: (row) => row.description ?? "-",
	},
	{
		label: "关联企业",
		//todo 缺失
		formatter: (row) => getCompanyName(row.enterpriseId) ?? "-",
	},
	{
		label: "创建时间",
		formatter: (row) => row.createTime ?? "-",
	},
	{
		label: "报送责任人",
		formatter: (row) => row.reportName ?? "-",
	},
	{
		label: "报送时间",
		formatter: (row) => row.reportTime ?? "-",
	},
	{
		label: "状态",
		formatter: (row) =>
			getEnumOptionsField(
				CREDIT_INFO_REPORT_REPORT_STATUS_OPTIONS,
				"label",
				row.reportStatus,
			) ?? "-",
	},

	{
		label: "操作",
		export: false,
		fixed: "right",
		width: "200",
		noExport: true,
		formatter(row) {
			return (
				<>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => onDelete(row)}
            v-show={row.reportStatus === '1'? false : true}
					>
						删除
					</el-button>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => detailDialogRef.value?.trigger("read", row)}
					>
						详情
					</el-button>
					<el-button
						size="small"
						type="primary"
						text
            v-show={row.reportStatus === '1'? false : true}
						onClick={() => detailDialogRef.value?.trigger("write", row)}
					>
						报送
					</el-button>
				</>
			);
		},
	},
];
</script>

<template>
	<TableWidget
		ref="tableRef"
		v-loading="isLoading"
		:data="records"
		:columns="columns"
		h="100%!"
		@selection-change="(rows:any) => (selectedList = toRaw(rows))"
	/>
</template>
